package org.kl.auditsupervision.web.workflow;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.kl.auditsupervision.entity.fnd.AuditDefect;
import org.kl.auditsupervision.entity.fnd.AuditProDtl;
import org.kl.auditsupervision.entity.fnd.EffectEvaluate;
import org.kl.auditsupervision.entity.fnd.ZgCs;
import org.kl.auditsupervision.service.fnd.AuditDefectService;
import org.kl.auditsupervision.service.fnd.AuditProDtlService;
import org.kl.auditsupervision.service.fnd.EffectEvaluateService;
import org.kl.auditsupervision.service.fnd.ZgCsService;
import org.kl.bf.entity.basic.Attachment;
import org.kl.bf.service.basic.AttachmentService;
import org.kl.bf.service.basic.DBProcessExecuteService;
import org.kl.bf.service.basic.UserService;
import org.kl.bf.web.base.BasicController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.MediaTypes;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 审批提交前过程
 * @author QJ
 *
 */
@Controller
@RequestMapping(value = "/wfl/beforePost")
public class BeforePostController extends BasicController {
		
	@Autowired
	private AuditProDtlService auditProDtlService;
	@Autowired
	private EffectEvaluateService effectEvaluateService;
	@Autowired
	private AttachmentService attachmentService;
	@Autowired
	private UserService userService;
	@Autowired
	private ZgCsService zgCsService;
	@Autowired
	private AuditDefectService auditDefectService;
	@Autowired
	private DBProcessExecuteService dBProcessExecuteService;
	
	
	/**
	 * 
	 * @param ASDept
	 * @return
	 */
	@RequestMapping(value = "ASDept", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String validateASDept(@RequestParam(value = "businessId",required = false) String businessId,
			@RequestParam(value = "operate",required = false) String operate,
			@RequestParam(value = "taskId",required= false) String taskId,
			HttpServletRequest request) {
		try{
			AuditProDtl auditProDtl=auditProDtlService.getOne(businessId);
			if(operate.equals("是")){
				Calendar cal = Calendar.getInstance();
				cal.add(Calendar.DATE, 10);
				Timestamp ts = new Timestamp(cal.getTimeInMillis());
				//ts=new Timestamp(new DateTime().getMillis());
				auditProDtl.setZgPlanSubTime(ts);
				auditProDtlService.save(auditProDtl);
			}
			//执行自己的逻辑
			return ajaxMsg("校验成功！", true, null);
		}catch(Exception e){
			e.printStackTrace();
			return ajaxMsg(e.getMessage(), false, null);
		}
	}
	
	/**
	 * 
	 * @param ZRCantonalLeaderAssign
	 * @return
	 */
	@RequestMapping(value = "ZRCantonalLeaderAssign", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String validateZRCantonalLeaderAssign(@RequestParam(value = "businessId",required = false) String businessId,
			@RequestParam(value = "operate",required = false) String operate,
			@RequestParam(value = "taskId",required= false) String taskId,
			HttpServletRequest request) {
		try{
			AuditProDtl auditProDtl=auditProDtlService.getOne(businessId);
			if(operate.equals("是")){				
				if(auditProDtl.getEmployee()==null){
					return ajaxMsg("请在业务信息中先填写负责人再提交", false, null);
				}else{
					
					return ajaxMsg("校验成功！", true, userService.findUserByEmployeeId(auditProDtl.getEmployee().getId()));					
				}
			}
			//执行自己的逻辑
			return ajaxMsg("校验成功！", true, null);
		}catch(Exception e){
			e.printStackTrace();
			return ajaxMsg(e.getMessage(), false, null);
		}
	}
	
	
	/**
	 * 
	 * @param 旧流程需要。。。
	 * @return
	 */
	@RequestMapping(value = "ZRLeaderAssign", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String validateZRLeaderAssign(@RequestParam(value = "businessId",required = false) String businessId,
			@RequestParam(value = "operate",required = false) String operate,
			@RequestParam(value = "taskId",required= false) String taskId,
			HttpServletRequest request) {
		try{
			AuditProDtl auditProDtl=auditProDtlService.getOne(businessId);
			if(operate.equals("是")){				
				if(auditProDtl.getEmployee()==null){
					return ajaxMsg("请在业务信息中先填写负责人再提交", false, null);
				}else{
					
					return ajaxMsg("校验成功！", true, userService.findUserByEmployeeId(auditProDtl.getEmployee().getId()));					
				}
			}
			//执行自己的逻辑
			return ajaxMsg("校验成功！", true, null);
		}catch(Exception e){
			e.printStackTrace();
			return ajaxMsg(e.getMessage(), false, null);
		}
	}
	
	/**
	 * 
	 * @param ZRMake
	 * @return
	 */
	@RequestMapping(value = "ZRMake", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String validateZRMake(@RequestParam(value = "businessId",required = false) String businessId,
			@RequestParam(value = "operate",required = false) String operate,
			@RequestParam(value = "taskId",required= false) String taskId,
			HttpServletRequest request) {
		try{
			
			if(operate.equals("是")){
				//AuditProDtl auditProDtl=auditProDtlService.getOne(businessId);
				//List<Attachment> attachmentList= attachmentService.findByAttachmentMasterIdAndAttachmentType(auditProDtl.getAttachmentMaster().getId(),"整改方案");
				List<ZgCs> zgCsList=zgCsService.findByAuditProDtlIdOrderByZgPlanTimeDesc(businessId);
				if(zgCsList.isEmpty()){
					return ajaxMsg("请在“制定整改方案”填写整改方案计划再提交", false, null);
				}
				zgCsList.get(0);
				AuditProDtl auditProDtl=auditProDtlService.getOne(businessId);
				Timestamp ts=new Timestamp(new DateTime().getMillis());
				auditProDtl.setZgPlanSubSjTime(ts);
				auditProDtl.setZgResultSubTime(new Timestamp(zgCsList.get(0).getZgPlanTime().getTime()+86399000));
				auditProDtlService.save(auditProDtl);
			}
			//执行自己的逻辑
			return ajaxMsg("校验成功！", true, null);
		}catch(Exception e){
			e.printStackTrace();
			return ajaxMsg(e.getMessage(), false, null);
		}
	}
	
	
	/**
	 * 
	 * @param ZRLeaderAppr
	 * @return
	 */
	@RequestMapping(value = "ZRLeaderAppr", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String validateZRLeaderAppr(@RequestParam(value = "businessId",required = false) String businessId,
			@RequestParam(value = "operate",required = false) String operate,
			@RequestParam(value = "taskId",required= false) String taskId,
			HttpServletRequest request) {
		try{
			
			if(operate.equals("是")){
				
			}else{
				AuditProDtl auditProDtl=auditProDtlService.getOne(businessId);
				return ajaxMsg("", true, userService.findUserByEmployeeId(auditProDtl.getEmployee().getId()));
			}
			//执行自己的逻辑
			return ajaxMsg("校验成功！", true, null);
		}catch(Exception e){
			e.printStackTrace();
			return ajaxMsg(e.getMessage(), false, null);
		}
	}
	
	/**
	 * 
	 * @param ASConfirm
	 * @return
	 */
	@RequestMapping(value = "ASConfirm", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String validateASConfirm(@RequestParam(value = "businessId",required = false) String businessId,
			@RequestParam(value = "operate",required = false) String operate,
			@RequestParam(value = "taskId",required= false) String taskId,
			HttpServletRequest request) {
		try{
			AuditProDtl auditProDtl=auditProDtlService.getOne(businessId);
			if(operate.equals("是")){				
				int planTimes=auditProDtl.getPlanTimes()==null?0:auditProDtl.getPlanTimes();
				if(planTimes==0){
					auditProDtl.setPlanTimes(planTimes);
					auditProDtlService.save(auditProDtl);
				}
			}else{
				int planTimes=auditProDtl.getPlanTimes()==null?0:auditProDtl.getPlanTimes();
				auditProDtl.setPlanTimes(planTimes+1);
				auditProDtlService.save(auditProDtl);
				return ajaxMsg("", true, userService.findUserByEmployeeId(auditProDtl.getEmployee().getId()));
			}
			//执行自己的逻辑
			return ajaxMsg("校验成功！", true, null);
		}catch(Exception e){
			e.printStackTrace();
			return ajaxMsg(e.getMessage(), false, null);
		}
	}
	
	/**
	 * 
	 * @param ZRExe
	 * @return             
	 */
	@RequestMapping(value = "ZRExe", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String validateZRExe(@RequestParam(value = "businessId",required = false) String businessId,
			@RequestParam(value = "operate",required = false) String operate,
			@RequestParam(value = "taskId",required= false) String taskId,
			HttpServletRequest request) {
		try{			
			if(operate.equals("是")){
				//AuditProDtl auditProDtl=auditProDtlService.getOne(businessId);
				//List<Attachment> attachmentList= attachmentService.findByAttachmentMasterIdAndAttachmentType(auditProDtl.getAttachmentMaster().getId(),"整改证据");
				List<ZgCs> zgCsList=zgCsService.findByAuditProDtlId(businessId);
				for(ZgCs zgCs:zgCsList){
					if(StringUtils.isBlank(zgCs.getZgEvidence())){
						return ajaxMsg("请在填写整改证据中填写“整改证据”再提交", false, null);
					}
				}
				/*if(attachmentList.isEmpty()){
				return ajaxMsg("请在业务信息中上传“整改证据”附件再提交", false, null);
				}*/
				
				List<EffectEvaluate> effectEvaluateList=effectEvaluateService.findByAuditProDtlIdAndStatusIsNull(businessId);
				if(effectEvaluateList.isEmpty()){
					return ajaxMsg("请在业务信息中填写自评表再提交", false, null);
				}else{
					
				}				
				AuditProDtl auditProDtl=auditProDtlService.getOne(businessId);
				Timestamp ts=new Timestamp(new DateTime().getMillis());
				auditProDtl.setZgResultSubSjTime(ts);
				auditProDtlService.save(auditProDtl);				
				dBProcessExecuteService.CreateCleanRisk(auditProDtl.getAuditPro().getId(), auditProDtl.getOrg().getId());
			}else{
				
			}
			return ajaxMsg("校验成功！", true, null);
		}catch(Exception e){
			e.printStackTrace();
			return ajaxMsg(e.getMessage(), false, null);
		}
	}
	
	
	
	/**
	 * 
	 * @param ASEva
	 * @return
	 */
	@RequestMapping(value = "ASEva", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String validateASEva(@RequestParam(value = "businessId",required = false) String businessId,
			@RequestParam(value = "operate",required = false) String operate,
			@RequestParam(value = "taskId",required= false) String taskId,
			HttpServletRequest request) {
		try{
			AuditProDtl auditProDtl=auditProDtlService.getOne(businessId);
			List<EffectEvaluate> effectEvaluateList=effectEvaluateService.findByAuditProDtlIdAndStatusIsNull(businessId);
			List<AuditDefect> auditDefectList=auditDefectService.findByAuditProDtlIdAndStatus(businessId,"0");
			if(operate.equals("是")){
				
				if(auditDefectList.isEmpty()){
					return ajaxMsg("请填写“审计复评”再提交", false, null);
				}else{
					for(AuditDefect entity:auditDefectList){
						entity.setStatus("1");
						auditDefectService.save(entity);
					}
					for(EffectEvaluate entity:effectEvaluateList){
						entity.setStatus("1");
						effectEvaluateService.save(entity);
					}
				}
				
				int resultTimes=auditProDtl.getResultTimes()==null?0:auditProDtl.getResultTimes();
				if(resultTimes==0){
					auditProDtl.setResultTimes(resultTimes);
					auditProDtlService.save(auditProDtl);
				}
			}else{
				if(auditProDtl.getIsCxzg()!=null&&auditProDtl.getIsCxzg().equals(1)){	
					if(auditDefectList.isEmpty()){
						return ajaxMsg("持续整改的项目，请填写“审计复评”再提交", false, null);
					}else{
						//------------生成缺陷表-------
						AuditDefect auditDefect=new AuditDefect();
						auditDefect.setAuditProDtl(auditProDtl);
						auditDefect.setCreateBy(auditProDtl.getCreateBy());
						auditDefect.setCreateDept(auditProDtl.getCreateDept());
						auditDefect.setCreateDeptName(auditProDtl.getCreateDeptName());
						auditDefect.setCreateMan(auditProDtl.getConfirmMan());
						auditDefect.setCreateTime(new Timestamp(new Date().getTime()));
						auditDefect.setZgqk("正在整改");
						AuditDefect entity2=auditDefectService.findByAuditProDtlIdOrderByYearDesc(auditProDtl.getId()).get(0);
						auditDefect.setYear((Integer.parseInt(entity2.getYear())+1)+"");
						auditDefectService.save(auditDefect);
						
						for(AuditDefect entity:auditDefectList){
							entity.setStatus("1");
							auditDefectService.save(entity);
						}
						for(EffectEvaluate entity:effectEvaluateList){
							entity.setStatus("1");
							effectEvaluateService.save(entity);
						}
					}				
				}else{
					int resultTimes=auditProDtl.getResultTimes()==null?0:auditProDtl.getResultTimes();
					auditProDtl.setResultTimes(resultTimes+1);
					auditProDtlService.save(auditProDtl);		
				}
						
				return ajaxMsg("", true, userService.findUserByEmployeeId(auditProDtl.getEmployee().getId()));
			}
			return ajaxMsg("校验成功！", true, null);
		}catch(Exception e){
			e.printStackTrace();
			return ajaxMsg(e.getMessage(), false, null);
		}
	}
}
